﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Excel = Microsoft.Office.Interop.Excel;
using System.Reflection;
using System.IO;
using LiquidacionComision.LiquidacionesService;

namespace LiquidacionComision.Controllers
{
    [HandleError]
    public class ReporteController : Controller
    {
        LiquidacionesService.LiquidacionesClient liquidacionService = new LiquidacionesService.LiquidacionesClient();

        public ActionResult Index()
        {
            ViewData["Message"] = "Welcome to ASP.NET MVC!";

            return View();
        }

        public ActionResult About()
        {
            return View();
        }

        public ActionResult ReporteResumen()
        {
            return View();
        }

        public ActionResult ReporteDetallado()
        {
            return View();
        }

        public ActionResult ReporteResumenDownload()
        {

                if (System.IO.File.Exists(Server.MapPath("/Upload").ToString()+"/ReporteResumenDownload.xls"))
                {
                    System.IO.File.Delete(Server.MapPath("/Upload").ToString()+"/ReporteResumenDownload.xls");
                }
                       
                Excel.Application MyApp = new Excel.Application();
                MyApp.Visible = false;
                //Excel.Workbook MyBook = MyApp.Workbooks.Open(@"D:\Excel\Output.xls");
                Excel.Workbook MyBook = MyApp.Workbooks.Open(Server.MapPath("/Upload") + "/libro.xls");
                Excel.Worksheet MySheet = (Excel.Worksheet)MyBook.Sheets[1]; 
                int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;

                //Obtener los datos
                string periodoInicio = (Request.QueryString["fechaInicio"] == null) ? string.Empty : Request.QueryString["fechaInicio"];
                string periodoFin = (Request.QueryString["fechaFin"] == null) ? string.Empty : Request.QueryString["fechaFin"];

                List<ReporteResumen> lista = liquidacionService.ListarReporteResumen(periodoInicio, periodoFin).ToList<ReporteResumen>();
                
                MySheet.Cells[1, 1] = "Periodo";
                MySheet.Cells[1, 2] = "Agente";
                MySheet.Cells[1, 3] = "Monto Liquidación";

                foreach(ReporteResumen reporte in lista){
                    lastRow += 1;
                    MySheet.Cells[lastRow, 1] = reporte.Periodo;
                    MySheet.Cells[lastRow, 2] = reporte.Agente;
                    MySheet.Cells[lastRow, 3] = reporte.MontoLiquidacion;
                }        
            
                MyBook.SaveAs(Server.MapPath("/Upload") + "/ReporteResumenDownload.xls");
                MyBook.Close();
            return null;
        }

        public ActionResult ReporteDetalladoDownload()
        {
            if (System.IO.File.Exists(Server.MapPath("/Upload").ToString() + "/ReporteDetalladoDownload.xls"))
            {
                System.IO.File.Delete(Server.MapPath("/Upload").ToString() + "/ReporteDetalladoDownload.xls");
            }

            Excel.Application MyApp = new Excel.Application();
            MyApp.Visible = false;
            Excel.Workbook MyBook = MyApp.Workbooks.Open(Server.MapPath("/Upload") + "/libro.xls");
            Excel.Worksheet MySheet = (Excel.Worksheet)MyBook.Sheets[1];
            int lastRow = MySheet.Cells.SpecialCells(Excel.XlCellType.xlCellTypeLastCell).Row;

            //Obtener los datos
            string periodoInicio = (Request.QueryString["fechaInicio"] == null) ? string.Empty : Request.QueryString["fechaInicio"];
            string periodoFin = (Request.QueryString["fechaFin"] == null) ? string.Empty : Request.QueryString["fechaFin"];
            string filtro = (Request.QueryString["filtro"] == null) ? string.Empty : Request.QueryString["filtro"];

            List<Liquidacion> lista = liquidacionService.ObtenerReporteDetallado(periodoInicio, periodoFin, filtro).ToList<Liquidacion>();

            MySheet.Cells[1, 1] = "Periodo";
            MySheet.Cells[1, 2] = "Agente";
            MySheet.Cells[1, 3] = "Categoria";
            MySheet.Cells[1, 4] = "CantidadOT";
            MySheet.Cells[1, 5] = "Precio";
            MySheet.Cells[1, 6] = "Monto Liquidación";

            foreach (Liquidacion reporte in lista)
            {
                lastRow += 1;
                MySheet.Cells[lastRow, 1] = reporte.Periodo;
                MySheet.Cells[lastRow, 2] = reporte.Agente;
                MySheet.Cells[lastRow, 3] = reporte.Categoria;
                MySheet.Cells[lastRow, 4] = reporte.CantidadOT;
                MySheet.Cells[lastRow, 5] = reporte.Precio;
                MySheet.Cells[lastRow, 6] = reporte.MontoLiquidacion;
            }

            MyBook.SaveAs(Server.MapPath("/Upload") + "/ReporteDetalladoDownload.xls");
            MyBook.Close();
            return null;
        }
    }
}
